package org.oschina.api.user.mail.controller;

import java.util.List;
import org.oschina.api.BaseController;
import org.oschina.api.user.mail.model.UserMailType;
import org.oschina.api.user.mail.service.UserMailService;
import org.oschina.api.user.mail.service.UserMailTypeService;
import org.oschina.api.user.mail.vo.UserMailCountVo;
import org.oschina.api.user.mail.vo.UserMailTypeVo;
import org.oschina.api.user.mail.vo.UserMailVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.HtmlCleanFileter;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/user/mail")
public class UserMailController extends BaseController {

	@Autowired
	private UserMailService userMailService;
	@Autowired
	private UserMailTypeService userMailTypeService;
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return userMailService.delete(id);
	}
	
	/**
	 * 设置已读
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/read/{id}",method=RequestMethod.POST)
	public Result setRead
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return userMailService.setRead(id);
	}
	
	/**
	 * 返回未读邮件数量
	 * @return
	 */
	@RequestMapping(value="/unread",method=RequestMethod.GET)
	public List<UserMailCountVo> getUnReadCount()
	{
		return userMailService.getUnReadCount();
	}
	
	/**
	 * 根据令牌和类型返回
	 * @param type
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/token",method=RequestMethod.GET)
	public Pager<UserMailVo> findByToken
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer type,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return userMailService.findByToken(type, pn);
	}
	
	/**
	 * 创建邮件
	 * @param title
	 * @param content
	 * @param target
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String title,
		@Validate(validateField=@ValidateField(filter={HtmlCleanFileter.class})) String content,
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String target
	)
	{
		return userMailService.create(title, content, target);
	}
	
	/**
	 * 返回类型
	 * @return
	 */
	@RequestMapping(value="/type",method=RequestMethod.GET)
	public List<UserMailTypeVo> findType()
	{
		return userMailTypeService.findVo();
	}
	
	/**
	 * 更新类型
	 * @param userMail
	 * @return
	 */
	@Validate
	@RequestMapping(value="/type/update/{id}",method=RequestMethod.POST)
	public Result update
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="name",filter={TextOnlyFilter.class}),
			@ValidateField(name="text",filter={TextOnlyFilter.class})
		}) UserMailType userMailType
	)
	{
		return userMailTypeService.update(userMailType);
	}
	
	/**
	 * 返回系统邮件
	 * @param pn
	 * @return
	 */
	@RequestMapping(value="/system",method=RequestMethod.GET)
	public Pager<UserMailVo> findSystem(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return userMailService.findSystem(pn);
	}

	/**
	 * 创建系统邮件
	 * @param title
	 * @param content
	 * @param role
	 * @return
	 */
	@Validate
	@RequestMapping(value="/system/create",method=RequestMethod.POST)
	public Result createSystem
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String title,
		@Validate(validateField=@ValidateField(filter={HtmlCleanFileter.class})) String content,
		@Validate(validateField=@ValidateField(validateValue=true,min=0)) Integer role
	)
	{
		return userMailService.createSystem(title, content, role);
	}
}
